<?

class MailSMTP extends Core {

	protected static $instancia;
	private $host;
	private $port;
	private $username;
	private $password;
	private $_mailsmtp = null;
	private $headers = array(
		'X-Mailer' => 'Microsoft Office Outlook 11',
		'Thread-Index' => 'Ack6CWSQlgV8s6+6SWyifka2NNpB7g==',
		'X-MimeOLE' => 'Produced By Microsoft MimeOLE V6.00.2900.3350'
	);


	function construct() {
		parent::construct();

		$this->host = CONFIG_SMTP_HOST;
		$this->port = CONFIG_SMTP_PORT;
		$this->username = CONFIG_SMTP_USER;
		$this->password = CONFIG_SMTP_PASS;

	}

	public static function getInstance() {
		if (!self::$instancia instanceof self)
			self::$instancia = new self;
		return self::$instancia;
	}

	private function smtp() {
		if (!$this->_mailsmtp) {
			// Incluyo la libreria de pear para mandar mails
			//require_once CONFIG_DOCUMENT_ROOT."includes/pear/Mail/Mail.php";
//			require_once "Mail/mail.php";
			require_once "Mail.php";

			// Creo una instancia de mail smtp
			$this->_mailsmtp = Mail::factory('smtp',
				array (
					'host' => $this->host,
					'port' => $this->port,
					'auth' => true,
					'username' => $this->username,
					'password' => $this->password
				)
			);
		}
		return $this->_mailsmtp;
	}

	public function send ($to, $body, $_headers=null) {
		// Seteo los headers
		$headers = array();
		if ($_headers)
			$headers = is_array($_headers)? $_headers : preg_split("/\r\n|\n/", $_headers);
		// Le agrego los encabezados generales
		//$headers = array_merge($headers, $this->headers);
		$headers['To'] = $to;
		$smtp = $this->smtp();
		$mail = $smtp->send($to, $headers, $body);

		if (PEAR::isError($mail)) {
echo $mail->getMessage();
			$this->Session->setFlash($mail->getMessage());
			return false;
		}
		return true;
	}

}
?>
